﻿@{
    ViewData["Title"] = "Index";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        var wireTransfer, remittanceAccount, cryptoCurrency
        var fromCurrency, depositCurrency
        var channelId
        var mothod = 1
        var price
        var renderTable;
        var userType = 0;
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            treetable: 'treetable-lay/treetable',
            index: 'lib/index'
        }).use(['table', 'form', 'treetable', 'index', 'upload'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , upload = layui.upload
                , table = layui.table
                , element = layui.element
                , treetable = layui.treetable;
            top.SetListPageLanguage(layui);

            //电汇账户开始
            $.getJSON("/Assets/WireTransfer", function (result) {
                let plat = result.data;
                wireTransfer = result.data;
                plat.forEach(function (v) {
                    $('#wireTransferCurrency').append(new Option(v.currency, v.id));
                });
                if (result.data.length > 0) {
                    setFromCurrency($('select[name="wireTransferCurrency"] option:selected').val(), wireTransfer[0].currency)
                    $('#payee').html(wireTransfer[0].payee);
                    $('#payeenumber').html(wireTransfer[0].accountNumber);
                    $('#payeeaddress').html(wireTransfer[0].payeeAddress);
                    $('#payeebank').html(wireTransfer[0].bank);
                    $('#swiftcode').html(wireTransfer[0].swiftcode);
                    $('#bankaddress').html(wireTransfer[0].bankAddress);
                }
                layui.form.render("select");
            });

            form.on('select(wireTransferCurrency)', function (data) {
                wireTransfer.forEach(function (v) {
                    if (v.id == data.value) {
                        setFromCurrency(data.value, v.currency)
                        $('#payee').html(v.payee);
                        $('#payeenumber').html(v.accountNumber);
                        $('#payeeaddress').html(v.payeeAddress);
                        $('#payeebank').html(v.bank);
                        $('#swiftcode').html(v.swiftcode);
                        $('#bankaddress').html(v.bankAddress);
                    }
                });
                form.render();
            });
            //电汇账户结束

            //汇款账户开始
            $.getJSON("/Assets/RemittanceAccount", function (result) {
                let plat = result.data;
                remittanceAccount = result.data;
                plat.forEach(function (v) {
                    $('#remittanceAccountCurrency').append(new Option(v.currency, v.id));
                });
                if (result.data.length > 0) {
                    $('#accountname').html(remittanceAccount[0].payee);
                    $('#accountnumber').html(remittanceAccount[0].accountNumber);
                    $('#bank').html(remittanceAccount[0].bank);
                    $('#branchname').html(remittanceAccount[0].branchName);
                    $('#qrcode').attr("src", remittanceAccount[0].qrcode);
                }
                layui.form.render("select");
            });

            form.on('select(remittanceAccountCurrency)', function (data) {
                remittanceAccount.forEach(function (v) {
                    if (v.id == data.value) {
                        setFromCurrency(data.value, v.currency)
                        $('#accountname').html(v.payee);
                        $('#accountnumber').html(v.accountNumber);
                        $('#bank').html(v.bank);
                        $('#branchname').html(v.branchName);
                        $('#qrcode').attr("src", v.qrcode);
                    }
                });
                form.render();
            });
            //汇款账户结束

            //加密货币
            $.getJSON("/Assets/Cryptocurrency", function (result) {
                let plat = result.data;
                cryptoCurrency = result.data;
                plat.forEach(function (v) {
                    $('#cryptoCurrency').append(new Option(v.currency, v.id));
                });
                if (result.data.length > 0) {
                    $('#network').html(cryptoCurrency[0].network);
                    $('#receivingaddress').html(cryptoCurrency[0].receivingAddress);
                }
                layui.form.render("select");
            });

            form.on('select(cryptoCurrency)', function (data) {
                cryptoCurrency.forEach(function (v) {
                    if (v.id == data.value) {
                        setFromCurrency(data.value, v.currency)
                        $('#network').html(v.network);
                        $('#receivingaddress').html(v.receivingAddress);
                    }
                });
                form.render();
            });
            //加密货币

            $.getJSON("/Assets/GetMt5User", function (result) {
                let plat = result.data;
                deposit = result.data;
                plat.forEach(function (v) {
                    $('#mt5user').append(new Option(v.login + "(" + v.balance.toFixed(2) + v.currency + ")", v.login));
                });
                setDepositcurrency(deposit[0].currency)
                layui.form.render("select");
            });

            //TAB切换
            element.on('tab(deposit)', function (elem) {
                var selectVal;
                mothod = elem.index + 1
                if (elem.index == 0) {
                    fromCurrency = $('select[name="wireTransferCurrency"] option:selected').text()
                    selectVal = $('select[name="wireTransferCurrency"] option:selected').val()
                }
                else if (elem.index == 1) {
                    fromCurrency = $('select[name="remittanceAccountCurrency"] option:selected').text()
                    selectVal = $('select[name="remittanceAccountCurrency"] option:selected').val()
                }
                else if (elem.index == 2) {
                    fromCurrency = $('select[name="cryptoCurrency"] option:selected').text()
                    selectVal = $('select[name="cryptoCurrency"] option:selected').val()
                }
                setFromCurrency(selectVal, fromCurrency)
            });

            $("#depositamount").blur(function () {
                getDepositAmount(fromCurrency, depositCurrency, $('#depositamount').val())
            });

            //设置来源币种
            function setFromCurrency(channel, currency) {
                console.log(channel, currency)
                channelId = channel
                fromCurrency = currency
                $("#fromcurrency").html(currency)
                getExchangeRate(fromCurrency, depositCurrency)
            }

            //设置来源币种
            function setDepositcurrency(currency) {
                depositCurrency = currency
                $("#depositcurrency").html(currency)
                getExchangeRate(fromCurrency, depositCurrency)
            }

            //获取汇率
            function getExchangeRate(fromCurrency, depositCurrency) {
                //console.log(fromCurrency, depositCurrency)
                if (fromCurrency != null && depositCurrency != null) {
                    $.ajax({
                        type: "POST",
                        url: "/Assets/GetExchangeRate",
                        data: {
                            "from": fromCurrency,
                            "to": depositCurrency
                        },
                        async: false,
                        error: function (request) {
                            alert("Connection error");
                        },
                        success: function (data) {
                            var item = eval(data);
                            if (item.code == 0) {
                                if (item.data != null) {
                                    price = item.data.price
                                    $('#exchangerate').val(item.data.from + '/' + item.data.to + '=' + item.data.price)
                                }
                            }
                            else {
                                top.ShowMessageByKey(layui, item.data, 0, 1000);
                                $('#exchangerate').val('')
                            }
                        }
                    });
                    getDepositAmount(fromCurrency, depositCurrency, $('#depositamount').val())
                }
            }

            //获取汇率
            function getDepositAmount(fromCurrency, depositCurrency, amount) {
                if (fromCurrency != null && depositCurrency != null) {

                    $.ajax({
                        type: "POST",
                        url: "/Assets/GetRealAmount",
                        data: {
                            "from": fromCurrency,
                            "to": depositCurrency,
                            "amount": amount
                        },
                        async: false,
                        error: function (request) {
                            alert("Connection error");
                        },
                        success: function (data) {
                            var item = eval(data);
                            if (item.code == 0) {
                                $('#fees').val(item.data.commission)
                                $('#fromamount').val(item.data.payAmount)
                                $('#depositamountshow').html(item.data.payAmount)
                            }
                            else {
                                top.ShowMessageByKey(layui, item.data, 0, 1000);
                            }
                        }
                    });
                }
            }

            //入金提交
            form.on('submit(submitDeposit)', function (data) {
                //var channelId = $("#wireTransferCurrency option:selected").val()
                //var mothod = mothod
                var mt5Id = $("#mt5user option:selected").val()
                var depositamount = $("#depositamount").val()
                var depositcurrency = $("#depositcurrency").text()
                var fees = $("#fees").val()
                var fromamount = $("#fromamount").val()
                var fromcurrency = $("#fromcurrency").text()
                var certificate = $("#certificate").attr('src')
                var password = $("#password").val()
                var remarks = $("#remark").val()
                console.log(channelId, mothod, mt5Id, depositamount, depositcurrency, fees, fromamount, fromcurrency, certificate, password, remarks)
                $.ajax({
                    type: "POST",
                    url: "/Assets/Deposit",
                    data: {
                        "channelId": channelId,
                        "mothod": mothod,
                        "mt5Id": mt5Id,
                        "fees": fees,
                        "depositcurrency": depositcurrency,
                        "fromcurrency": fromcurrency,
                        "depositamount": depositamount,
                        "fromamount": fromamount,
                        "certificate": certificate,
                        "password": password,
                        "remarks": remarks,
                    },
                    async: false,
                    error: function (request) {
                        alert("Connection error");
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            top.ShowMessageByKey(layui, item.data, 6, 1000, function () {
                                location.reload();
                            });
                        } else {
                            top.ShowMessageByKey(layui, item.data, 0, 1000);
                        }
                    }
                });
                return false;
            });

            $("#gotoSetTradePassword").click(function () {
                parent.layui.index.openTabsPage("/Personal/TradePassword");
                //初始化导航选项卡（选中新建标签卡）
                parent.layui.element.render('nav');
            });

            //拖拽上传
            upload.render({
                elem: '#uploadCertificate'
                , url: '/Base/UploadFile' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                , done: function (res) {
                    //console.log(res.data.src)
                    layer.msg('上传成功');
                    $('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.data.src);

                }
            });

            //图片预览事件
                $('#qrcode').click(function () {
                    previewImg(this);
                });

            //图片预览事件
            $('#uploadDemoView img').click(function () {
                previewImg(this);
            });
        });



    </script>
}
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
        @if (ViewBag.noTradePassword)
        {
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body" style="text-align:center;">
                        <blockquote class="layui-elem-quote" id="setTradePassword" lay-event="setTradePassword">请先设置您的资金密码</blockquote>
                        <button type="button" class="layui-btn" id="gotoSetTradePassword" lay-event="gotoSetTradePassword">前往设置资金密码</button>
                    </div>
                </div>
            </div>
        }
        else
        {
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <div class="layui-tab layui-tab-brief" lay-filter="deposit">
                            <ul class="layui-tab-title">
                                <li lay-event="wireTransfer" class="layui-this">wireTransfer</li>
                                <li lay-event="remittanceAccount">remittanceAccount</li>
                                <li lay-event="cryptoCurrency">cryptoCurrency</li>
                            </ul>
                            <div class="layui-tab-content">
                                <div class="layui-tab-item layui-show">
                                    <form class="layui-form">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="currencytitle">收款货币</label>
                                            <div class="layui-input-block">
                                                <select name="wireTransferCurrency" id="wireTransferCurrency" lay-filter="wireTransferCurrency">
                                                </select>
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="payeetitle">收款人</label>
                                            <label class="layui-form" style="padding-left:0px; text-align:left;" name="payee" id="payee"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="payeenumbertitle">收款账户</label>
                                            <label class="layui-form" style="padding-left:0px; text-align:left;" name="payeenumber" id="payeenumber"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="payeeaddresstitle">收款人地址</label>
                                            <label class="layui-form" style="padding-left:0px; text-align:left;" name="payeeaddress" id="payeeaddress"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="payeebanktitle">收款银行</label>
                                            <label class="layui-form" style="padding-left:0px; text-align:left;" name="payeebank" id="payeebank"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="swiftcodetitle">SWIFT Code</label>
                                            <label class="layui-form" style="padding-left:0px; text-align:left;" name="swiftcode" id="swiftcode"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="bankaddresstitle">收款银行地址</label>
                                            <label class="layui-form" style="padding-left:0px; text-align:left;" name="bankaddress" id="bankaddress"></label>
                                        </div>
                                    </form>
                                </div>
                                <div class="layui-tab-item">
                                    <form class="layui-form">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="currencytitle">收款货币</label>
                                            <div class="layui-input-block">
                                                <select name="remittanceAccountCurrency" id="remittanceAccountCurrency" lay-filter="remittanceAccountCurrency">
                                                </select>
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="accountnametitle">开户姓名</label>
                                            <label class="layui-form-label" style="padding-left:0px; text-align:left;" name="accountname" id="accountname"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="accountnumbertitle">银行卡号</label>
                                            <label class="layui-form-label" style="padding-left:0px; text-align:left;" name="accountnumber" id="accountnumber"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="banktitle">收款银行</label>
                                            <label class="layui-form-label" style="padding-left:0px; text-align:left;" name="bank" id="bank"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="branchnametitle">支行名称</label>
                                            <label class="layui-form-label" style="padding-left:0px; text-align:left;" name="branchname" id="branchname"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="qrcodetitle">收款二维码</label>
                                            <img class="head-img" name="qrcode" id="qrcode" width="96" height="96">
                                            @*<label class="layui-form-label" style="padding-left:0px; text-align:left;" name="qrcode" id="qrcode"></label>*@
                                        </div>
                                    </form>
                                </div>
                                <div class="layui-tab-item">
                                    <form class="layui-form">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="currencytitle">收款货币</label>
                                            <div class="layui-input-block">
                                                <select name="cryptoCurrency" id="cryptoCurrency" lay-filter="cryptoCurrency">
                                                </select>
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="networktitle">网络</label>
                                            <label class="layui-form-label" style="padding-left:0px; text-align:left;" name="network" id="network"></label>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label" lay-event="receivingaddresstitle">收款账户</label>
                                            <label class="layui-form-label" style="padding-left:0px; text-align:left;" name="receivingaddress" id="receivingaddress"></label>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
                        </fieldset>
                        <form class="layui-form">
                            <div class="layui-form-item">
                                <label class="layui-form-label">入金账号</label>
                                <div class="layui-input-block">
                                    <select name="mt5user" id="mt5user" lay-filter="mt5user">
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-group">
                                    <div class="layui-input-prefix">入金金额</div>
                                    <input type="text" placeholder="带任意前置和后置内容" class="layui-input" id="depositamount" name="depositamount">
                                    <div class="layui-input-split layui-input-suffix" id="depositcurrency" name="depositcurrency">
                                        USD
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">入金汇率</label>
                                <div class="layui-input-block">
                                    <input type="text" name="exchangerate" id="exchangerate" class="layui-input" disabled>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-group">
                                    <div class="layui-input-prefix">预计手续费</div>
                                    <input type="text" placeholder="带任意前置和后置内容" class="layui-input" id="fees" name="fees" disabled>
                                    <div class="layui-input-split layui-input-suffix" id="feescurrency" name="feescurrency">
                                        USD
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-group">
                                    <div class="layui-input-prefix">收款金额</div>
                                    <input type="text" placeholder="带任意前置和后置内容" class="layui-input" id="fromamount" name="fromamount" disabled>
                                    <div class="layui-input-split layui-input-suffix" id="fromcurrency" name="fromcurrency">
                                        USD
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">转账凭证</label>
                                <div class="layui-upload-drag" id="uploadCertificate">
                                    <i class="layui-icon"></i>
                                    <div class="layui-hide" id="uploadDemoView">
                                        <hr>
                                        <img src="" style="max-width: 196px" id="certificate">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">资金密码</label>
                                <div class="layui-input-block">
                                    <input type="password" autocomplete="off" placeholder="资金密码" class="layui-input" id="password">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">备注</label>
                                <div class="layui-input-block">
                                    <input type="text" autocomplete="off" placeholder="备注" class="layui-input" id="remark">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">请支付</label>
                                <div class="layui-input-block">
                                    <div class="layui-card-body crm-depositamount" id="depositamountshow" name="depositamountshow">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button type="submit" class="layui-btn" lay-submit lay-filter="submitDeposit">立即提交</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        }
    </div>
</div>